package com.qiantu.phone.widget;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextPaint;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.phone.R;
import com.qiantu.phone.app.AppApplication;

public class StickyItemDecoration extends RecyclerView.ItemDecoration {

    private Paint paint;
    private RectF currentFloatRect;
    //开始上移的高度，也是需要移动高度
    private float startTranslateHeight;
    private int offset;
    private int dp16;

    public StickyItemDecoration() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(AppApplication.getInstance().getThemeColor(R.attr.secondaryColor3));
        offset = AppApplication.getInstance().getResources().getDimensionPixelSize(R.dimen.dp_4);
        dp16 = AppApplication.getInstance().getResources().getDimensionPixelSize(R.dimen.dp_16);
    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
        //第一个可见条目的位置
        View firstItem = parent.getChildAt(0);
        if(firstItem == null){
            return;
        }
        TextView firstTimeDay = firstItem.findViewById(R.id.tv_time_day);
        if(firstTimeDay == null){
            return;
        }
        if(currentFloatRect == null){
            int bottom = firstTimeDay.getBottom() + AppApplication.getInstance().getResources().getDimensionPixelSize(R.dimen.dp_13);
            currentFloatRect = new RectF();
            currentFloatRect.left = dp16;
            currentFloatRect.top = firstItem.getPaddingTop();
            currentFloatRect.right = firstItem.getWidth() - dp16;
            currentFloatRect.bottom = bottom;
            //因为item有marginBottom 4dp
            startTranslateHeight = currentFloatRect.height() - offset;
        }
        View secondItem = null;
        TextView secondTimeDay = null;
        for (int i = 1; i < parent.getChildCount(); i++) {
            View item = parent.getChildAt(i);
            if(item == null){
                return;
            }
            TextView timeDay = item.findViewById(R.id.tv_time_day);
            if(timeDay != null && timeDay.getVisibility() == View.VISIBLE){
                secondItem = item;
                secondTimeDay = timeDay;
                break;
            }
        }
        if((firstTimeDay != null && firstTimeDay.getTop() <= 0)
                && (secondItem == null || secondItem.getTop() > startTranslateHeight)){
//            c.drawRoundRect(currentFloatRect, dp16, dp16, paint);
            c.drawRect(currentFloatRect, paint);
            TextPaint mTextPaint = firstTimeDay.getPaint();
            mTextPaint.setColor(AppApplication.getInstance().getThemeColor(R.attr.textColor1));
            Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
            c.drawText(firstTimeDay.getText().toString(), firstTimeDay.getPaddingLeft(), -fontMetricsInt.top + firstTimeDay.getPaddingTop(), mTextPaint);
        }else if(secondItem != null && secondItem.getTop() <= startTranslateHeight){
            float dy = startTranslateHeight - secondItem.getTop();
            TextView timeDay = null;
            //当上距离超过需要移动的高度，就停止移动并且画第2个label
            if(dy > startTranslateHeight){
                timeDay = secondTimeDay;
            }else{
                c.translate(0, -dy);
                timeDay = firstTimeDay;
            }
//            c.drawRoundRect(currentFloatRect, dp16, dp16, paint);
            c.drawRect(currentFloatRect, paint);
            TextPaint mTextPaint = timeDay.getPaint();
            mTextPaint.setColor(AppApplication.getInstance().getThemeColor(R.attr.textColor1));
            Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
            c.drawText(timeDay.getText().toString(), timeDay.getPaddingLeft(), -fontMetricsInt.top + timeDay.getPaddingTop(), mTextPaint);
        }
    }
}
